package com.koron.standard.patrolLine.controller;

import cn.hutool.poi.excel.WorkbookUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koron.bean.base.Response;
import com.koron.common.core.business.common.service.TOrderNoMaxService;
import com.koron.standard.bean.query.PageQuery;
import com.koron.standard.excel.SameRowMargeStrategy;
import com.koron.standard.patrolLine.bean.dto.LinePatrolDTO;
import com.koron.standard.patrolLine.bean.excel.LinePatrolExcel;
import com.koron.standard.patrolLine.bean.query.LinePatrolQuery;
import com.koron.standard.patrolLine.bean.vo.LinePatrolVO;
import com.koron.standard.patrolLine.excel.LinePatrolExcelListener;
import com.koron.standard.patrolLine.service.LinePatrolCheckService;
import com.koron.standard.patrolLine.service.LinePatrolRecordService;
import com.koron.standard.patrolLine.service.LinePatrolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * 巡视标准(Patrol)表控制层
 *
 * @author jzp
 * @since 2021-10-19
 */
@Api(tags = "线路巡视标准接口")
@RestController
@RequestMapping("patrolLine")
public class LinePatrolController {

    @Autowired
    private LinePatrolService linePatrolService;

    @Autowired
    LinePatrolCheckService linePatrolCheckService;

    @Autowired
    LinePatrolRecordService linePatrolRecordService;

    @Autowired
    TOrderNoMaxService tOrderNoMaxService;


    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<Page<LinePatrolVO>> page(PageQuery<LinePatrolVO> pageQuery, LinePatrolQuery query) {
        Page<LinePatrolVO> page = linePatrolService.page(pageQuery, query);
        return Response.succ(page, page.getTotal());
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<LinePatrolDTO> select(@RequestParam String id) {
        LinePatrolDTO linePatrolDTO = linePatrolService.getDtoById(id);
        return Response.ok(linePatrolDTO);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public Response save(@RequestBody LinePatrolDTO linePatrolDTO) {
        linePatrolService.saveDto(linePatrolDTO);
        return Response.ok(linePatrolDTO.getId());
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public Response update(@RequestBody LinePatrolDTO linePatrolDTO) {
        linePatrolService.updateDtoById(linePatrolDTO);
        return Response.ok();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping
    public Response delete(@RequestParam("ids") List<String> ids) {
        linePatrolService.removeByIds(ids);
        return Response.ok();
    }

    @ApiOperation(value = "设置状态")
    @PutMapping("updateStatus")
    public Response updateStatus(@RequestParam String id, @RequestParam Integer status) {
        linePatrolService.updateStatus(id, status);
        return Response.ok();
    }

    @ApiOperation(value = "设置为公共标准")
    @PutMapping("updateToPublic")
    public Response updateToPublic(@RequestParam("ids") List<String> ids) {
        linePatrolService.updateToPublic(ids);
        return Response.ok();
    }

    @SneakyThrows
    @ApiOperation(value = "下载模板")
    @GetMapping("template")
    public void template(HttpServletResponse response) {
        String path = "excel/line_patrol_standard.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        Workbook book = WorkbookUtil.createBook(classPathResource.getInputStream());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("巡视标准", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        book.write(response.getOutputStream());
    }

    @SneakyThrows
    @ApiOperation(value = "导入")
    @PostMapping("import")
    public Response importFile(@RequestParam("file") MultipartFile file) {
        EasyExcel.read(file.getInputStream(), LinePatrolExcel.class, new LinePatrolExcelListener(linePatrolService, linePatrolCheckService, linePatrolRecordService, tOrderNoMaxService))
                .sheet("巡视标准")
                .headRowNumber(2)
                .doRead();

        return Response.ok("导入成功", null);
    }

    @SneakyThrows
    @ApiOperation(value = "导出", notes = "根据条件导出查询出的所有数据")
    @GetMapping("export")
    public void exportFile(HttpServletResponse response, LinePatrolQuery query) {
        String path = "excel/line_patrol_standard_export.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("巡视标准", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<LinePatrolExcel> typeExcelList = linePatrolService.listPatrolExcel(query);
        EasyExcel.write(response.getOutputStream())
                .withTemplate(classPathResource.getInputStream())
                .sheet("巡视标准")
                .registerWriteHandler(new SameRowMargeStrategy<LinePatrolExcel>(typeExcelList, new HashSet<>(Arrays.asList(0, 1, 2)), LinePatrolExcel::getTypeId))
                .doFill(typeExcelList);
    }

}
